package tools;

import java.io.*;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.xml.sax.InputSource;

public class DealXml {
	
	private String _xmlStr;
	private Document _document=null;
	private StringReader sr = null;
	private InputSource is = null;
	private SAXBuilder sb = null;
	
	public String GetXmlStr(){
		String sXml=null;
    	try{
    		XMLOutputter   out   =   new   XMLOutputter(); 
    		sXml=out.outputString(_document); 

    	}catch(Exception e){
    		e.printStackTrace();
    	}
		return sXml;
	}
	
	public String GetString(Element el)
	{
		String sXml=null;
    	try{
    		XMLOutputter   out   =   new   XMLOutputter(); 
    		sXml=out.outputString(el); 

    	}catch(Exception e){
    		e.printStackTrace();
    	}
		return sXml;
	}
	
	public DealXml()
	{
	}
	
	public DealXml(String xmlStr){
		InitXmlString(xmlStr);
	}
	
	public DealXml(String s, boolean isFile){
		if (!isFile) InitXmlString(s);
		else{
			InitXmlFile(s);
		}
	}
	
	//判断解析是否成功
	public  boolean IsParseOK(){
		return _document!=null;
	}
	
	private void InitXmlFile(String path)
	{
		SAXBuilder builder = new SAXBuilder();
		try{
			InputStream is=this.getClass().getResourceAsStream(path);
			_document = builder.build(is);
		}catch(JDOMException e){
    		try{
    			_document = builder.build(new File(path));
    		}catch(Exception sube){
    			e.printStackTrace();
    			sube.printStackTrace();
    		}
    		
    	}catch(IOException e) {
    		e.printStackTrace();
    	}
	}
	
	private void InitXmlString(String xmsStr)
	{
		_xmlStr=xmsStr;
    	sr = new StringReader(_xmlStr);
    	is = new InputSource(sr);
    	sb = new SAXBuilder();
    	try{
    		_document = sb.build(is);
    		/*List<Element> inputs = _document.getRootElement().getChildren("input");
    		for(int i=0; i < inputs.size(); i++)
    		{
    			Element el = (Element)inputs.get(i);
    			System.out.println(el.getName());
    		}*/
    	}catch(JDOMException e){
    		e.printStackTrace();
    	}catch(IOException e) {
    		e.printStackTrace();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
	}
	
	 /*读取XML格式的字符串，根据属性来查找值
	    * @param xmlStr 是要解析的xml格式的字符串
	    * @param sProperty 是在xml字符串中要获得标签属性
	    * @return 返回的标签属性是name的值
	 */
		public String GetRootPropertyValue(String sProperty){
	    	String value = "";
	    	try{
	    		Element e = _document.getRootElement(); 
	    		value = e.getAttributeValue(sProperty);
	    	}catch(Exception e) {
	    		e.printStackTrace();
	    	}
	    	return value;
	   }
		
		public void SetRootPropertyValue(String sKey, String sValue)
		{
	    	try{
	    		Element e = _document.getRootElement();
	    		Attribute att = e.getAttribute(sKey);
	    		if (att==null) e.setAttribute(sKey, sValue);
	    		else {
	    			att.setValue(sValue);
	    		}
	    	}catch(Exception e) {
	    		e.printStackTrace();
	    	}
		}
	    
	    public String GetRootChildValue(String sName){
	    	String value = "";
	    	try{
	    		Element e = _document.getRootElement().getChild(sName); 
	    		if (e!=null) value = e.getText();
	    	}catch(Exception e) {
	    		e.printStackTrace();
	    	}
	    	return value;
	    }
	    
	    public String GetRootChildPropertyValue(String sName, String sProperty){
	    	String value = "";
	    	try{
	    		Element e = _document.getRootElement().getChild(sName); 
	    		if (e!=null) value = e.getAttributeValue(sProperty);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    	}
	    	return value;
	    }
	    
	    public Element GetRootElement(){
	    	return _document.getRootElement();
	    }
	    
	    public Element IsHave(String timescale,String type,String element){
	    	Element e =GetRootElement().getChild(timescale);
	    	
	    	if(e == null) return null;    	
	    	if(type == null) return e;	    	
			if (e.getChild(type) == null) return null;
			if(element == null) return e.getChild(type);
			if (e.getChild(type).getChild(element) == null) return null;
	    	return e.getChild(type).getChild(element);
	    }
}